package nccloud.web.hr.login.action;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.pub.BusinessException;
import nc.vo.sm.UserVO;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.IAuthenticateAction;
import nccloud.framework.web.container.IHttpOperator;
import nccloud.framework.web.json.JsonFactory;
import nccloud.utils.mobile_riart.MessageLogger;
import nccloud.web.hr.login.util.HRBindSSOLoginUtil;
import nccloud.web.hr.login.util.SSOLoginUtil;
import nccloud.web.hr.login.util.UCGPwdProcessUtil;
import nccloud.web.hr.login.util.WebSSOLoginUtil;
import nccloud.web.hr.pub.HRNccPubEnv;

/**
 * HR移动应用单点登录
 */
public class HRLoginVerfiyAction implements IAuthenticateAction {

	@Override
	public Object doAction(IHttpOperator paramOperator) {
		MessageLogger.info("第三方调用移动端审批通过【nccloud.web.hr.login.action.HRLoginVerfiyAction】，开始调用NCC移动端审批接口...........");
		String str = paramOperator.getRequest().read();
		IJson json = JsonFactory.create();
		Map<String,Object> param = json.fromJson(str, HashMap.class);
		String dataSource = (String) param.get("bcCode");
		HRNccPubEnv.ensureSecurity(dataSource);
		HRBindSSOLoginUtil ssoLoginUtil = new HRBindSSOLoginUtil();
		try {
			//获取账套编码
			ssoLoginUtil.getBusiCenterVO(dataSource, param);
			//操作符
			String operator = (String) param.get("operator");
			//通过操作符
			if (StringUtils.isBlank(operator)) {
				//单点登录获取友空间用户信息 mod by chenjma 友空间获取用户信息改为本地获取
				//              ssoLoginUtil.getUserInfoYkj(param);
				//获取单点登录用户
				String usercode = (String) param.get("usercode");
				if(usercode==null) {
					throw new nccloud.framework.core.exception.BusinessException("单点登录参数usercode[用户编码]不能为空！");
				}
				IMDPersistenceQueryService mdQryService = ServiceLocator.find(IMDPersistenceQueryService.class);
				Collection<UserVO> usercol = mdQryService.queryBillOfVOByCond(UserVO.class, " dr=0 and USER_CODE='" + usercode + "'", true);
				if(usercol!=null && usercol.size()>0) {
					UserVO uservo = usercol.toArray(new UserVO[0])[0];
					param.put("user_code", usercode);
					param.put("cuserid",uservo.getCuserid());
					String pk_psndoc = uservo.getPk_psndoc();
					if(pk_psndoc!=null) {
						PsndocVO psnvo = mdQryService.queryBillOfVOByPK(PsndocVO.class, pk_psndoc, true);
						param.put("mobile", psnvo.getMobile());
					}
				}else {
					MessageLogger.info("第三方调用移动端审批通过【nccloud.web.hr.login.action.HRLoginVerfiyAction】，单点登录用户[" + usercode + "]验证失败，系统不存在！");
					throw new nccloud.framework.core.exception.BusinessException("单点登录用户[" + usercode + "]验证失败，系统不存在！");
				}

				return new SSOLoginUtil().doLogin(paramOperator, param);
			} else {
				if ("init".equals(operator) || "send".equals(operator) || "bind".equals(operator)) {//绑定初始化的时候，获取友空间用户信息
					ssoLoginUtil.getUserInfoYkj(param);
					return new BindYHTUtil().doBind(param);
				} else if ("websso".equals(operator)) {
					return new WebSSOLoginUtil().doLogin(paramOperator, param);
				} else if ("ucgpwd".equals(operator)){
					String pwd = (String) param.get("pwd");
					return new UCGPwdProcessUtil().getRsaPwdJson(pwd);
				}
			}
		} catch (BusinessException e) {
			MessageLogger.info("第三方调用移动端审批通过【nccloud.web.hr.login.action.HRLoginVerfiyAction】，开始调用NCC移动端审批接口...........");
			throw new nccloud.framework.core.exception.BusinessException(e.getMessage());
		}
		return null;
	}
}
